/********************************************************************
	created:	1:10:2007   2:36
	author:		tiamo
	purpose:	debug
*********************************************************************/

#include "stdafx.h"
#include <stdarg.h>
#include <stdio.h>
#include <ntstrsafe.h>
#include <ntintsafe.h>

#ifdef DBG
	ULONG g_ulDbgFlags													= DBGLEVEL_ERROR | DBGLEVEL_INFO | DBGLEVEL_WARNING | DBGLEVEL_NONE;
#else
	ULONG g_ulDbgFlags													= DBGLEVEL_ERROR;
#endif

// debug break
VOID AppleDbgBreakPoint()
{
	if(KdDebuggerEnabled)
	{
		DbgBreakPoint();
	}
	else
	{
		__try
		{
			DbgBreakPoint();
		}
		__except(EXCEPTION_EXECUTE_HANDLER)
		{

		}
	}
}

// debug print
VOID AppleDebugPrint(__in ULONG ulLevel,__in PCCH pFormat,...)
{
	if(!(ulLevel & g_ulDbgFlags))
		return;

	va_list list;
	va_start(list,pFormat);

	CHAR buffer[1025] = {0};

	if(NT_SUCCESS(RtlStringCchVPrintfA(buffer,ARRAYSIZE(buffer) - 1, pFormat,list)))      
		DbgPrint(buffer);
	else
		DbgPrint("Buffer overrun while formating the debug statement");

	va_end(list);
}

// trace collection info
VOID AppleTraceCollectionInfo(__in ULONG ulLevel,__in PHID_COLLECTION_INFORMATION pInfo)
{
	if(!(ulLevel & g_ulDbgFlags))
		return;

	AppleDebugPrint(ulLevel,"AppleKeyboard collection info\n");
	AppleDebugPrint(ulLevel,"\tVendorId  : %04x\n",pInfo->VendorID);
	AppleDebugPrint(ulLevel,"\tProductId : %04x\n",pInfo->ProductID);
	AppleDebugPrint(ulLevel,"\tVersion   : %04x\n",pInfo->VersionNumber);
	AppleDebugPrint(ulLevel,"\tPolled    : %s\n",pInfo->Polled ? "true" : "false");
	AppleDebugPrint(ulLevel,"\tDescSize  : %08x\n",pInfo->DescriptorSize);
}

// trace collection data
VOID AppleTraceCollectionData(__in ULONG ulLevel,__in PVOID pDataBuffer,__in ULONG ulLength)
{
	if(!(ulLevel & g_ulDbgFlags))
		return;

	PUCHAR pData = static_cast<PUCHAR>(pDataBuffer);
	AppleDebugPrint(ulLevel,"AppleKeyboard preparsed data (%08x)\n",ulLength);

	for(ULONG i = 0; i < ulLength; i ++)
	{
		if(i % 8 == 0)
			AppleDebugPrint(ulLevel,"%08x :",i / 8);

		AppleDebugPrint(ulLevel," %02x",pData[i]);

		if((i + 1) % 8 == 0)
			AppleDebugPrint(ulLevel,"\n");
	}

	if(ulLength % 8)
		AppleDebugPrint(ulLevel,"\n");
}

// trace caps
VOID AppleTraceHidCaps(__in ULONG ulLevel,__in PHIDP_CAPS pCaps)
{
	if(!(ulLevel & g_ulDbgFlags))
		return;

	AppleDebugPrint(ulLevel,"AppleKeyboard hid caps\n");
	AppleDebugPrint(ulLevel,"\tFeatureReportByteLength   : %04x\n",pCaps->FeatureReportByteLength);
	AppleDebugPrint(ulLevel,"\tInputReportByteLength     : %04x\n",pCaps->InputReportByteLength);
	AppleDebugPrint(ulLevel,"\tNumberFeatureButtonCaps   : %04x\n",pCaps->NumberFeatureButtonCaps);
	AppleDebugPrint(ulLevel,"\tNumberFeatureDataIndices  : %04x\n",pCaps->NumberFeatureDataIndices);
	AppleDebugPrint(ulLevel,"\tNumberFeatureValueCaps    : %04x\n",pCaps->NumberFeatureValueCaps);
	AppleDebugPrint(ulLevel,"\tNumberInputButtonCaps     : %04x\n",pCaps->NumberInputButtonCaps);
	AppleDebugPrint(ulLevel,"\tNumberInputDataIndices    : %04x\n",pCaps->NumberInputDataIndices);
	AppleDebugPrint(ulLevel,"\tNumberInputValueCaps      : %04x\n",pCaps->NumberInputValueCaps);
	AppleDebugPrint(ulLevel,"\tNumberLinkCollectionNodes : %04x\n",pCaps->NumberLinkCollectionNodes);
	AppleDebugPrint(ulLevel,"\tNumberOutputButtonCaps    : %04x\n",pCaps->NumberOutputButtonCaps);
	AppleDebugPrint(ulLevel,"\tNumberOutputDataIndices   : %04x\n",pCaps->NumberOutputDataIndices);
	AppleDebugPrint(ulLevel,"\tNumberOutputValueCaps     : %04x\n",pCaps->NumberOutputValueCaps);
	AppleDebugPrint(ulLevel,"\tOutputReportByteLength    : %04x\n",pCaps->OutputReportByteLength);
	AppleDebugPrint(ulLevel,"\tUsage                     : %04x\n",pCaps->Usage);
	AppleDebugPrint(ulLevel,"\tUsagePage                 : %04x\n",pCaps->UsagePage);
}